package com.yang.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yang.model.BaseResult;
import com.yang.model.User;
import com.yang.service.UserService;

@Controller
public class LoginController {
	
	@Autowired
	private UserService userService;
	
	 @RequestMapping("/login")
	    public String login() {			
	    	return "index";
		}
	 
	 @RequestMapping("dologin")
	    @ResponseBody
	    public Object dologin(User user,HttpSession session) {
	    	BaseResult<User> result =new BaseResult<>();	    	
	    	User loginUser = userService.doLogin(user);	    	
	    	if (loginUser !=null) {
	    		session.setAttribute("loginUser", loginUser);
	    		result.setSuccess(true);
		    	result.setData(loginUser);
			}else {
				result.setSuccess(false);
		    	result.setData(loginUser);
			}	    
	    	return result;
	    }
	    @RequestMapping("main")
	    public String toMain() {			
	    	return "main";
		}
}
